package com.threeti.mecool.common.resource.core;

import java.io.IOException;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;

/** 文本内容资源 */
public class DefaultPlainTextContent extends DefaultContent {

	public DefaultPlainTextContent(Resource fsRes) {
		super(fsRes);
	}

	@SuppressWarnings("unchecked")
	/** 转换为普通的文本字符*/
	public String toPlainText() throws IOException {
		if (!(getFilename().endsWith(".txt") || getFilename().endsWith(".TXT")) && 
				!(getFilename().endsWith(".html")|| getFilename().endsWith(".HTML"))) {//TODO jay:more flexible here
			throw new UnsupportedOperationException("资源不支持文本转换，所支持的格式有txt,html");
		}
		List<String> txtContents = IOUtils.readLines(getInputStream());
		StringBuffer buf = new StringBuffer();
		for (String content : txtContents) {
			buf.append(content);
		}
		return buf.toString();
	}

}
